package com.shun.servlet;

import com.shun.pojo.Ly;
import com.shun.service.LyService;
import com.shun.service.UserService;
import com.shun.service.impl.LyServiceImpl;
import com.shun.service.impl.UserServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet("/loginServlet")
public class loginServlet extends HttpServlet {
    private UserService userService=new UserServiceImpl();
    private LyService lyService=new LyServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //调用业务层
        String name = userService.login(username, password);
        List<Ly> list = lyService.selectAll();

        if(name!=null){
            //存入session
            request.getSession().setAttribute("username",username);
            request.getSession().setAttribute("list",list);
            request.getRequestDispatcher("/welcome.jsp").forward(request,response);
        }else{
            request.getSession().setAttribute("msg","用户名或密码错误！");
            request.getRequestDispatcher("/login.jsp").forward(request,response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
